package com.meixiang.adapter.service;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.meixiang.R;
import com.meixiang.entity.services.Beautician;
import com.meixiang.recyclerview.RecyclerAdapter;
import com.meixiang.tool.Tool;
import com.meixiang.view.CircleImageView;
import com.meixiang.view.LabelLayout;
import com.meixiang.view.StarLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 美服务-预约-项目 适配器
 */
public class CosmetologistAdapter extends RecyclerAdapter<CosmetologistAdapter.ViewHolder> {
    private Context mContext;
    public List<Beautician.ListEntity> data=new ArrayList<>();
    private Fragment fragment;

    public CosmetologistAdapter(Fragment fragment) {

        this.fragment = fragment;
    }
    public void addData(List<Beautician.ListEntity> data){
        this.data.addAll(data);
        notifyDataSetChanged();
    }
    public void clearData(){
        data.clear();
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_item_cosmetologist, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Glide.with(fragment)
                .load(data.get(position).getPhotoImage())
                .asBitmap()
                .error(R.drawable.image_default_gray_bg)
                .placeholder(R.drawable.image_default_gray_bg)
                .fitCenter()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        holder.ivHeadPortrait.setImageBitmap(resource);
                    }
                });
        holder.tvName.setText(data.get(position).getName());
        holder.labels.setLabels(data.get(position).getLabelList());
        holder.llStars.setStarNumber(data.get(position).getStar());
       /* if (Tool.toDouble(data.get(position).getStar())){
            holder.llStars.setStarNumber((int) Double.parseDouble(data.get(position).getStar()));
        }else{
            holder.llStars.setStarNumber(0);
        }*/
        if (Tool.toInteger(data.get(position).getServiceNum())){
            holder.tvServiceNumber.setText("服务" + data.get(position).getServiceNum() + "次");
        }else{
            holder.tvServiceNumber.setText("服务0次");
        }

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_head_portrait)
        CircleImageView ivHeadPortrait;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.ll_stars)
        StarLayout llStars;
        @Bind(R.id.labels)
        LabelLayout labels;
        @Bind(R.id.tv_service_number)
        TextView tvServiceNumber;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
